package dtaskboard.client.toolbar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import dtaskboard.client.Constants;
import dtaskboard.client.DialogUtil;
import dtaskboard.client.service.LoginService;
import dtaskboard.client.service.LoginServiceAsync;

public class ChangePasswordDialog extends DialogBox {

  private final TextBox oldPassword;
  private final TextBox newPassword;
  private final TextBox newPassword2;

  public ChangePasswordDialog() {
    this.oldPassword = new PasswordTextBox();
    this.newPassword = new PasswordTextBox();
    this.newPassword2 = new PasswordTextBox();

    Panel panel = new VerticalPanel();

    Label oldPassLabel = new Label("Senha antiga");
    Label newPassLabel = new Label("Senha nova");
    Label newPassLabel2 = new Label("Repita a senha nova");

    panel.add(oldPassLabel);
    panel.add(this.oldPassword);
    panel.add(newPassLabel);
    panel.add(this.newPassword);
    panel.add(newPassLabel2);
    panel.add(this.newPassword2);
    panel.add(new ChangePasswordButton());

    super.setAnimationEnabled(true);
    super.setAutoHideEnabled(true);
    super.setHTML("Troca de senha");
    super.setGlassEnabled(true);
    super.setModal(true);
    super.setWidget(panel);
    super.setVisible(true);
  }

  @Override
  public void show() {
    this.oldPassword.setText("");
    this.newPassword.setText("");
    this.newPassword2.setText("");
    super.show();
  }

  class ChangePasswordButton extends Button {
    public ChangePasswordButton() {
      this.setText("Alterar senha");
      this.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
          LoginServiceAsync loginService = GWT.create(LoginService.class);
          final ChangePasswordDialog cpd = ChangePasswordDialog.this;
          String sessionId = Cookies.getCookie(Constants.sessionIdCookie);
          if (!cpd.newPassword.getText().equals(cpd.newPassword2.getText())) {
            DialogUtil.closableDialog("Troca de senha",
                "Novas senhas diferentes.");
            return;
          }


          loginService.changePassword(cpd.oldPassword.getText(),
              cpd.newPassword.getText(), sessionId, new AsyncCallback<Void>() {

                @Override
                public void onSuccess(Void result) {
                  DialogUtil
                      .closableDialog("Troca de senha", "Senha alterada.");
                  cpd.hide();
                }

                @Override
                public void onFailure(Throwable caught) {
                  DialogUtil.closableDialog("Troca de senha",
                      "Erro ao trocar senha.");
                }
              });

        }
      });

    }
  }

}
